define([], function () {
  'use strict'
  // 校验表格
  function validateTable(data, columns, preMsg = '') {
    function defaultValidate({ row, column }) {
      const cellValue = $._.get(row,column.field)
      return $.Utils.isExist(cellValue)
    }
    for (let rowIndex = 0; rowIndex < data.length; rowIndex++) {
      const row = data[rowIndex]
      for (let columnIndex = 0; columnIndex < columns.length; columnIndex++) {
        const column = columns[columnIndex]
        if (column.children) {
          if (!validateTable(data, column.children)) {
            return false
          }
        }
        if (!column.field) continue
        if (!column.required && !column.validate) continue
        const validateFn = column.validate ?? defaultValidate
        const validate = validateFn({ row, rowIndex, column, columnIndex })
        if (typeof validate === 'object') {
          if (!validate.validate) {
            return false
          }
        }
        if (validate === false) {
          $.msg.tips(`${preMsg}请填写第【${rowIndex + 1}】行-${column.title}`)
          return false
        }
      }
    }
    return true
  }
  return {
    /**
     * 检验表格数据重复
     * @param {any[]} data 数据 
     * @param {number} tipIndex 索引
     * @param {string} key 键(可以为path)
     * @param {any} value 
     * @returns {Boolean} 是否验证成功
     */
    validateTabledata(data, tipIndex, key, value) {
      for (let index = 0; index < data.length; index++) {
        if (index === tipIndex) {
          continue
        }
        const cellValue = $._.get(data[index],key)
        if (cellValue === value) {
          return false
        }
      }
      return true
    },
    validateTable
  }
})